using System;
using System.Collections.Generic;
using System.Text;
using BuildingBasicDomain ;


namespace BuildingStructuralElements.Loads
{
    /// <summary>
    /// uniform presure Load on an Area 
    /// </summary>
    public class SurfaceLoad:Load 
    {
        public int GLid;
        public bool needsUpdate = true;
        private ILine[] lines;
        private IArea _area;
        private double[] pressure;

        /// <summary>
        /// the uniform destributed load 
        /// </summary>
        public double[] Pressure
        {
            get
            {
                if (pressure == null)
                    pressure = new double [6];
                return pressure ;
            }

            set
            {
                pressure = value;
            }
        }

        ///<summary>
        /// Constructor
        /// </summary>
        /// <param name="_area">An Area of destribution</param>
        public SurfaceLoad(IArea area)
        {
            _area = area;
        }

        public SurfaceLoad() { }

        public SurfaceLoad(SurfaceLoad surfaceload)
        {
            this.LC = surfaceload.LC;
            this.pressure = surfaceload.pressure;
        }
        public IArea Area
        {
            set
            {
                _area = value;
            }

            get
            {
                return _area;
            }
        }

        public ILine[] Lines
        {
            set
            {
                lines = value;
            }

            get
            {
                return lines;
            }
        }

        private DistributedLoad DestributeOn(ILine line)
        {
            DistributedLoad destributedload;
            NodalLoad uniformload = new NodalLoad(0, TotalLoad(2) / line.Length(), 0, 0, 0, 0);
            destributedload = new DistributedLoad(uniformload, line);
            return destributedload;
        }

        public override Load[] Distribute()
        {
            Load[] loads;
            if (lines == null) return null;

            if (lines.Length == 1)
            {
                loads = new Load[1];
                loads[0] = DestributeOn(lines[0]);
                return loads;
            }

            if (lines.Length > 1)
            {
                loads = new Load[lines.Length];
            /*    Area[] _areas = Area.destributeOnBeams(lines);
                TestArea(_areas);
                for (int i = 0; i < lines.Length - 2; i++)
                {
                    if (_areas[i] != null)
                    {
                        SurfaceLoad surfaceLoad = new SurfaceLoad(_areas[i]);
                        surfaceLoad.Pressure = Pressure;
                        surfaceLoad.Lines = new ILine[1];
                        surfaceLoad.Lines[0] = lines[i + 1];
                        loads[i] = surfaceLoad;
           
                    }
                }  * */
                return loads;
            }

            return null;


        }

        private void TestArea(Area[] _areas)
        {
            double totalArea = 0;
            foreach (Area _area in _areas)
            {
                totalArea += _area.GetArea();
            }
            double initialArea = Area.GetArea();
            if (totalArea != initialArea)
            {
                // throw new Exception(" Area not preserved ");}
            }
        }

        public override double TotalLoad(int direction)
        {
            double Total;
            Total = Area.GetArea() * Pressure[direction ];
            return Total;
        }
    }
}
